// 音频提示工具

interface AudioConfig {
  volume?: number;
  playbackRate?: number;
}

// 音频文件路径
const AUDIO_SOURCES = {
  success: '/sounds/success.mp3',
  error: '/sounds/error.mp3',
  notification: '/sounds/notification.mp3'
};

// 创建音频元素
function createAudioElement(src: string, config: AudioConfig = {}): HTMLAudioElement {
  const audio = new Audio(src);
  audio.volume = config.volume ?? 0.7;
  audio.playbackRate = config.playbackRate ?? 1.0;
  audio.preload = 'auto';
  return audio;
}

// 播放成功音效
export function playSuccessSound(config?: AudioConfig): Promise<void> {
  return new Promise((resolve, reject) => {
    try {
      const audio = createAudioElement(AUDIO_SOURCES.success, config);
      
      audio.onended = () => {
        resolve();
      };
      
      audio.onerror = (error) => {
        console.warn('播放成功音效失败:', error);
        resolve(); // 即使失败也不阻塞流程
      };
      
      audio.play().catch(error => {
        console.warn('播放成功音效失败:', error);
        resolve(); // 即使失败也不阻塞流程
      });
    } catch (error) {
      console.warn('创建成功音效失败:', error);
      resolve(); // 即使失败也不阻塞流程
    }
  });
}

// 播放错误音效
export function playErrorSound(config?: AudioConfig): Promise<void> {
  return new Promise((resolve, reject) => {
    try {
      const audio = createAudioElement(AUDIO_SOURCES.error, config);
      
      audio.onended = () => {
        resolve();
      };
      
      audio.onerror = (error) => {
        console.warn('播放错误音效失败:', error);
        resolve();
      };
      
      audio.play().catch(error => {
        console.warn('播放错误音效失败:', error);
        resolve();
      });
    } catch (error) {
      console.warn('创建错误音效失败:', error);
      resolve();
    }
  });
}

// 播放通知音效
export function playNotificationSound(config?: AudioConfig): Promise<void> {
  return new Promise((resolve, reject) => {
    try {
      const audio = createAudioElement(AUDIO_SOURCES.notification, config);
      
      audio.onended = () => {
        resolve();
      };
      
      audio.onerror = (error) => {
        console.warn('播放通知音效失败:', error);
        resolve();
      };
      
      audio.play().catch(error => {
        console.warn('播放通知音效失败:', error);
        resolve();
      });
    } catch (error) {
      console.warn('创建通知音效失败:', error);
      resolve();
    }
  });
}

// 播放自定义音频文件
export function playCustomSound(audioUrl: string, config?: AudioConfig): Promise<void> {
  return new Promise((resolve, reject) => {
    try {
      const audio = createAudioElement(audioUrl, config);
      
      audio.onended = () => {
        resolve();
      };
      
      audio.onerror = (error) => {
        console.warn('播放自定义音效失败:', error);
        resolve();
      };
      
      audio.play().catch(error => {
        console.warn('播放自定义音效失败:', error);
        resolve();
      });
    } catch (error) {
      console.warn('创建自定义音效失败:', error);
      resolve();
    }
  });
}

// 震动反馈（如果支持）
export function vibrate(pattern: number | number[] = 100): void {
  if ('vibrate' in navigator) {
    try {
      navigator.vibrate(pattern);
    } catch (error) {
      console.warn('震动反馈失败:', error);
    }
  }
}

// 组合反馈：声音 + 震动
export async function playSuccessFeedback(config?: AudioConfig): Promise<void> {
  vibrate([100, 50, 100]); // 成功震动模式
  await playSuccessSound(config);
}

export async function playErrorFeedback(config?: AudioConfig): Promise<void> {
  vibrate([200, 100, 200]); // 错误震动模式
  await playErrorSound(config);
}

export async function playNotificationFeedback(config?: AudioConfig): Promise<void> {
  vibrate(50); // 通知震动模式
  await playNotificationSound(config);
} 